//
//  HomeService.m
//  HxgProject
//
//  Created by hxg on 2018/3/26.
//  Copyright © 2018年 XMYY-19. All rights reserved.
//

#import "HomeService.h"
#import "NetworkManager.h"
#import "ResObject.h"
#import "PackageInfo.h"
#import "HxgInterfaceDefine.h"

#import "JK_BasicPackageInfo.h"

@implementation HomeService

- (void)getBaseBorrowPakageWithCallBack:(void(^)(ResObject *resObj,NSMutableArray *arr))callBack{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@%@",iP_Address,JK_Basic_kFindBasicInformation];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];
    params[@"name"] = kBasicInfoBorrowPackage;
    
    [man getURL:urlInfo parameters:params success:^(id responseObject) {
        NSLog(@"responseObject:%@",responseObject);
        
        NSDictionary *responDic = (NSDictionary *)responseObject;
        NSArray *arr = responDic[@"BasicInformationList"];
        if (arr
            && arr.count != 0) {
            NSMutableArray *returnArr = [[NSMutableArray alloc] init];
            for (NSDictionary *dic in arr) {
                JK_BasicPackageInfo *jkBasicPackage = [[JK_BasicPackageInfo alloc] initWithDic:dic];
                [returnArr addObject:jkBasicPackage];
            }
            
            ResObject *resObj = [[ResObject alloc] init];
            resObj.isSuccess = YES;
            callBack(resObj,returnArr);
        } else {
            ResObject *resObj = [[ResObject alloc] init];
            resObj.isSuccess = NO;
            callBack(resObj,nil);
        }
    } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
        NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
        ResObject *resObj = [[ResObject alloc] init];
        resObj.isSuccess = NO;
        callBack(resObj,nil);
    }];
}

- (void)saveBaseBorrowPackage:(JK_BasicPackageInfo *)basicPackageInfo
                 withCallBack:(void(^)(ResObject *resObje))callBack {
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@%@",iP_Address,JK_Basic_kSaveLoanPackage];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];
    params[@"loanPackage"] = basicPackageInfo.moneyNum;
    params[@"loanperiod"] = basicPackageInfo.borrowDays;
    params[@"serviceCharge"] = basicPackageInfo.jk_basePackageId;
    
    ResObject *result = [[ResObject alloc] init];
    [man postURL:urlInfo parameters:params success:^(id responseObject) {
        NSLog(@"responseObject:%@",responseObject);
        if ([[responseObject objectForKey:@"Success"]  isEqual: kRequestSuccess])
        {
            result.isSuccess = YES;
            callBack(result);
        } else {
            result.isSuccess = NO;
            result.errMsg = [responseObject objectForKey:@"errMsg"];
            callBack(result);
        }
    } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
        NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
        result.isSuccess = NO;
        result.errMsg = @"请求失败!";
        callBack(result);
    }];
}

@end
